﻿using System;
using System.Web.UI.WebControls;
using MPBlog.Core;
using MPBlog.DAL;
using Wuqi.Webdiyer;

namespace MPBlog.UserControls
{
    public class ArticleListBase : UCBase
    {
        #region - controls define -

        protected Repeater rptArticleList;
        protected AspNetPager aspNetArticlePager;


        #endregion

        #region - variable -

        private readonly ArticleDao articleDao = new ArticleDao();
        private readonly int pageSize = 5;
        private readonly string pageSortProperty;
        private readonly bool pageSortAsc = true;


        #endregion

        #region - event -

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected void RptArticleListItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "lbDelete")
            {
                int articleId = Convert.ToInt32(e.CommandArgument);
                articleDao.Delete(articleId);
            }
        }

        protected void OnAspNetPagerPageChanging(object src, PageChangingEventArgs e)
        {
            if (aspNetArticlePager != null)
            {
                aspNetArticlePager.RecordCount = 100;   //在设置currentpageindex之前必须指定recordCount，否则currenPageindex永远是1，可能是ASPNETPAGER的BUG
                aspNetArticlePager.CurrentPageIndex = e.NewPageIndex;
                LoadArticlePageList();
            }
        }

        #endregion

        #region - method -

        private void LoadArticlePageList()
        {
            int allRecordCount;
            rptArticleList.DataSource = articleDao.GetPage(aspNetArticlePager.CurrentPageIndex, pageSize, pageSortProperty, pageSortAsc, null, out allRecordCount);
            aspNetArticlePager.RecordCount = allRecordCount;
            aspNetArticlePager.PageSize = pageSize;
            rptArticleList.DataBind();
        }

        #endregion
    }
}